package com.lijy.superseckill.controller;

import com.lijy.superseckill.service.GoodsService;
import com.lijy.superseckill.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/to_list")
    public String list(Model model) {
       List<GoodsVO> goodsList = goodsService.listGoodsVO();
       model.addAttribute("goodsList", goodsList);
       return "goods_list";
    }

    @RequestMapping("/to_detail/{goodsId}")
    public String detail(Model model, @PathVariable long goodsId) {

        GoodsVO goods = goodsService.getGoodsVOByGoodsId(goodsId);
        model.addAttribute("goods", goods);

        long startTime = goods.getStartDate().getTime();
        long endTime = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int seckilStatus = 0;
        int remainSeconds = 0;

        if (now < startTime) { // 秒杀未开始
            seckilStatus = 0;
            remainSeconds = (int) (startTime - now)/1000;
        } else if (now > endTime) {// 秒杀已经结束
            seckilStatus = 2;
            remainSeconds = -1;
        } else { // 秒杀进行中
            seckilStatus = 1;
            remainSeconds = 0;
        }

        model.addAttribute("seckilStatus", seckilStatus);
        model.addAttribute("remainSeconds", remainSeconds);

        return "goods_detail";
    }

}
